package com.amqr.ammvvm.adapter;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.amqr.ammvvm.R;
import com.amqr.ammvvm.bean.UserBean;
import com.amqr.ammvvm.databinding.ItemRvBinding;

import java.util.List;

/**
 * User: LJM
 * Date&Time: 2017-12-18 & 16:31
 * Describe: Describe Text
 */
public class RvMmAdapter extends RecyclerView.Adapter<RvMmHolder>{

    private List<UserBean> mDatas;

    public interface IChanheClickListener{
        void changeClickListener(int position,UserBean userBean);
    }

    private IChanheClickListener mIChanheClickListener;
    public void setChangeClickListener(IChanheClickListener changeClickListener){
        this.mIChanheClickListener = changeClickListener;
    }


    public RvMmAdapter(List<UserBean> datas) {
        mDatas = datas;
    }

    @Override
    public RvMmHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemRvBinding itemRvBinding =DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_rv, parent, false);

        return new RvMmHolder(itemRvBinding);
    }


    @Override
    public void onBindViewHolder(RvMmHolder holder, final int position) {

        final ItemRvBinding itemRvBinding = holder.getItemRvBinding();
        final UserBean userBean = mDatas.get(position);
        itemRvBinding.setUser(userBean);
        itemRvBinding.mTvChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mIChanheClickListener!=null){
                    mIChanheClickListener.changeClickListener(position,userBean);
                }
            }
        });


        itemRvBinding.executePendingBindings();
    }


    @Override
    public int getItemCount() {
        return mDatas==null ? 0: mDatas.size();
    }
}
